package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageParamDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.ProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private ProjectPlanMapper projectPlanMapper;
    @Autowired
    private NursingLevelMapper nursingLevelMapper;


    //分页条件查询
    @Override
    public PageResponse<NursingPlanVo> getPage(NursingPlanPageParamDto nursingPlanPageParamDto) {
        //开启分页插件
        PageHelper.startPage(nursingPlanPageParamDto.getPageNum(), nursingPlanPageParamDto.getPageSize());
        //查询信息，封装为Page<>类型
        Page<List<NursingPlanVo>> result = nursingPlanMapper.selectByPage(nursingPlanPageParamDto);
        //返回结果
        return PageResponse.of(result,NursingPlanVo.class);
    }

    //查询所有
    @Override
    public List<NursingPlanVo> getAllPlan() {
        List<NursingPlanVo> result = nursingPlanMapper.selectAll();
        return result;
    }

    //新增计划
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {
        // 1、名称校验：不可为已存在的
        Long count = nursingPlanMapper.selectByName(nursingPlanDto.getPlanName());
        if (count > 0) {
            throw new BaseException(BasicEnum.PLAN_INSERT_FAIL_REPEAT_NAME);
        }
        // 2、新增计划：新增计划 + 为计划新增项目
        // 2.1 新增计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //状态赋值为启用
        nursingPlan.setStatus(1);
        nursingPlanMapper.insertPlan(nursingPlan);
        // 2.2 新增项目
        if (nursingPlan.getProjectPlans()!=null && !nursingPlan.getProjectPlans().isEmpty()) {
            // 1）SQL中主键（计划ID）已返回，绑定项目计划关联表中的计划id
            for (ProjectPlan projectPlan : nursingPlan.getProjectPlans()) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            // 2）批量新增项目计划关联表字段
            projectPlanMapper.insert(nursingPlan.getProjectPlans());
            // TODO 新增项目表 3）将项目计划关联表的project_id绑定到项目表的id

        }
    }

    //根据ID查询
    @Override
    public NursingPlanVo getPpById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectById(id);
        return nursingPlanVo;
    }

    //编辑计划信息
    @Override
    public void updatePlan(NursingPlanDto nursingPlanDto) {
        //使用NursingPlan实体类映射
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        //不可编辑已与护理等级绑定的计划
        Long count = nursingLevelMapper.selectPlanById(nursingPlan.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.PLAN_UPDATE_FAIL_LEVEL_EXIT);
        }
        //编辑计划信息
        nursingPlanMapper.update(nursingPlan);
        //删除全部关联项目信息
        projectPlanMapper.deleteByPlan(nursingPlan.getId());
        //批量添加关联项目信息
        if (nursingPlan.getProjectPlans()!=null && !nursingPlan.getProjectPlans().isEmpty()) { //不为空
            //为添加的每一个项目都与当前计划绑定
            for (ProjectPlan projectPlan : nursingPlan.getProjectPlans()) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            //批量添加关联项目信息
            projectPlanMapper.insert(nursingPlan.getProjectPlans());
        }
    }

    //删除计划
    @Override
    public void removePlan(Long id) {
        //不可删除与等级绑定的计划
        Long count = nursingLevelMapper.selectPlanById(id);
        if (count > 0) {
            throw new BaseException(BasicEnum.PLAN_DELETE_FAIL_LEVEL_EXIT);
        }
        //删除计划
        nursingPlanMapper.delete(id);
        //删除项目关联
        projectPlanMapper.deleteByPlan(id);
    }

    //启用、禁用
    @Override
    public void updatePlanStatus(Long id, Integer status) {
        //不可更改与等级绑定计划的状态
        Long count = nursingLevelMapper.selectPlanById(id);
        if (count > 0) {
            throw new BaseException(BasicEnum.PLAN_UPDATE_STATUS_FAIL_LEVEL_EXIT);
        }
        //修改计划状态
        nursingPlanMapper.updateStatusById(id,status);
    }


}
